???

Java ArrayList

The ArrayList in java is used to create an array of dynamic length.
In normal array, we need to specify the size of the array initially. So the array can only store the elements in it based on the size of the array.

With the help of ArrayList we can create an array that has no limit in size.

To use the ArrayList we need to import the java.util package.

Example for creating integer ArrayList

java import java.util.ArrayList; 
ArrayList<Integer> employeeNo = new ArrayList<Integer>(); 

Example to create string ArrayList

java import java.util.ArrayList; 
ArrayList<String> userNames= new ArrayList<String>(); 

Add Items to the ArrayList

The ArrayList class contains a method add() which is used to add an element to an arraylist.

Example program for adding element to arraylist

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> userNames = new ArrayList<String>();
    userNames.add("Andrew");
    userNames.add("Paul");
    userNames.add("John");
    userNames.add("Roger");
    System.out.println(userNames);
  }
}

Output

[Andrew, Paul, John, Roger]

In the above program, we have created an ArrayList userNames of type String. Then we use the add() method to add the values to the userNames and finally display the values.

Loop through ArrayList

It is possible to loop through an ArrayList variable and print the values in the array list.

Example program to loop through arraylist

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> userNames = new ArrayList<String>();
    userNames.add("Andrew");
    userNames.add("Paul");
    userNames.add("John");
    userNames.add("Roger");
    
    for(String user: userNames){
        System.out.println(user);
    }
  }
}

Output

Andrew
Paul
John
Roger

Get and Set the value in ArrayList

The ArrayList class contains two different methods to get and set the values in the arraylist.
The get() method is used to get the valu at a particular index position.
The set() methosd is used to set the value at particular index position.

Example to get and set the values in arraylist

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<String>();
    fruits.add("Apple");
    fruits.add("Grapes");
    fruits.add("Mango");
    fruits.add("Jack Fruit");

    // Set the value at index position 3
    fruits.set(3,"Papaya");
       
    // Get the element in index position 3
    System.out.println("Fruit at index position 3 is "+fruits.get(3));
    
  }
}

Output

Fruit at index position 3 is Papaya

Remove item(s) in an ArrayList

The ArrayList provides a method remove(), to remove the element at particular index position.

We use the clear() method to remove all the elements in the arraylist.

Example for removing item(s) in an arraylist

fruits.remove(1); // removes the element in index position 1.
fruits.clear(); // removes all the element in the fruits arraylist.

Size() method

The size() method is used to get the size of the arraylist.

Example for getting the size of the arraylist

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<String>();
    fruits.add("Apple");
    fruits.add("Grapes");
    fruits.add("Mango");
    fruits.add("Jack Fruit");
    
    for(int i=0;i<fruits.size();i++){
        System.out.println(fruits.get(i));
    }
  }
}

In the above program, we have created an ArrayList with the name fruits and add few elements to it and using the size() function we check the size of the fruits arraylist and print the elements.

Sort() method in ArrayList

The sort() method in Collections class, is used to sort the elements in ascending order. The Collections class is present in the java.util package.

Example for sort() method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(6);
    numbers.add(10);
    numbers.add(5);
    numbers.add(3);
    Collections.sort(numbers); // Sort the numbers arraylist.
    
    for(int x: numbers){
        System.out.println(x);
    }
  }
}

Output

3
5
6
10

Most Read